var RepairBoard = function () {
  var template = /* html */ `
    <div class="repair-board" v-loading.fullscreen.lock="loading">
      <search-btns :pagination="pagination"></search-btns>
      <template v-if="repairData!==null">
        <div class="repair-board-customer">
          <repair-customer-chart :repair-data="repairData" :pagination="pagination">
          </repair-customer-chart>
        </div>
        <div class="repair-board-parts">
          <repair-parts-chart :repair-data="repairData"></repair-parts-chart>
        </div>
      </template>
    </div>
  `;

  return {
    mixins: [boardRunningMixin],

    data: function () {
      return {
        loading: false,
        repairData: null,
        pagination: {
          currentPage: 1,
          totalCount: 0,
          pageSize: 10
        }
      };
    },

    created: function () {
      this.loading = true;
      var self = this;
      this.setRepairList(true, true).then(function () {
        self.boardRuning();
        self.loading = false;
      }).catch(function () {
        self.loading = false;
      });
    },

    methods: {
      setRepairList: function (hasAlert, isInit) {
        if (hasAlert === undefined) {
          hasAlert = false;
        }

        var self = this;
        return ajaxRequest('workOrder/getWorkRepairSummary', {}, true, hasAlert).then(function (res) {
          if (!isInit) {
            return res;
          }

          var repairData = new RepairInfo(res);
          self.repairData = repairData;
          self.pagination.totalCount = repairData.customerRepairData.length;
          return repairData;
        });
      },

      boardRuning: function () {
        this.clearTimeoutList();
        this.setRepairDataRunning();
      },

      setRepairDataRunning: function () {
        var tomorrowMoment = moment().add(1, 'day').startOf('day');
        var delayTime = tomorrowMoment.diff(moment());

        var self = this;
        var timeout = setTimeout(function () {
          self.setRepairList().then(function (res) {
            var repairData = self.repairData;
            repairData.resetInfo(res);

            var pagination = self.pagination;
            var totalCount = repairData.customerRepairData.length;
            var pageSize = pagination.pageSize;
            var totalPage = Math.ceil(_.round(totalCount / pageSize, 2));
            pagination.totalCount = totalCount;
            if (pagination.currentPage > totalPage) {
              pagination.currentPage = totalPage;
            }
            self.setRepairDataRunning();
          }).catch(function () {});
          self.timeoutList[0] = null;
        }, delayTime);
        this.timeoutList[0] = timeout;
      }
    },

    components: {
      SearchBtns: SearchBtns,
      RepairCustomerChart: RepairCustomerChart,
      RepairPartsChart: RepairPartsChart
    },

    template: template
  };
}();
